<?
include_once("_common.php");
include_once("$mw_cash[path]/cybercash.lib.php");

if (!$is_member)
    alert_close("로그인 후 이용하실 수 있습니다.");

//if (!$bo_table) alert_close("bo_table 이 없습니다.");
//include_once("$board_skin_path/mw.lib/mw.skin.basic.lib.php");
//if ($mw_basic[cf_contents_shop] != "2") alert_close("멤버쉽 설정을 먼저 해주세요.");

$g4[title] = "$board[bo_subject] 멤버쉽";
include_once("$g4[path]/head.sub.php");

$ms_one = false;
$mi_one = false;

ob_start();
?>

<style type="text/css">
body { padding:10px; }
h3 { font-weight:bold; font-size:13px; margin:0 0 10px 0; }
.contents-info { border:5px solid #efefef; padding:20px; font-weight:bold; font-size:13px; padding-left:20px; }
.contents-info td { height:30px; }
td.head { border-bottom:1px solid #ddd; padding:15px 10px 0 0; font-weight:bold; }
td.body { border-bottom:1px solid #ddd; padding:10px; }
.btn1 { background-color:#efefef; cursor:pointer; }
.btn2 { background-color:#fff; cursor:pointer; border:0; font-size:12px; color:#0000ff; }
.hints { color:#888; margin:5px 0 5px 0; }
.blue { color:#0D6AB2; }
.button { text-align:center; margin:30px 0 30px 0; }
.ms_memo { margin:10px 0 10px 0; border:1px solid #FFD700; background-color:#FFFFE0; padding:10px; }
#payment-info { color:#888; }
</style>

<script type="text/javascript" src="<?=$mw_cash[path]?>/cybercash.js"></script>
<script type="text/javascript">
function form_submit() {
    if (!confirm('정말 가입하시겠습니까?')) return false;

    var mi_day = false;

    if (fwrite.mi_day.length > 0) {
        for (i=0; i<fwrite.mi_day.length; i++) {
            if (fwrite.mi_day[i].checked) {
                mi_day = fwrite.mi_day[i].value;
                break;
            }
        }
    } else {
        mi_day = fwrite.mi_day.value;
    }
    if (!mi_day) {
        alert("기간을 선택해주세요.");
        return false;
    }
    return true;
}
</script>

<h3><img src="img/icon_cash.gif" align="absmiddle"> <?=$g4[title]?></h3>

<form name="fwrite" method="post" action="buy-membership-update.php" onsubmit="return form_submit()">
<input type="hidden" name="bo_table" value="<?=$bo_table?>">
<div class="contents-info">
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
    <tr><td>
        <?
        $mem = array();
        $sql = "select * from $mw_cash[membership_table] where ms_status = '1' order by ms_id";
        $qry = sql_query($sql);
        if (mysql_num_rows($qry) == 1) {
            $row = sql_fetch_array($qry);
            $mem = $row;
            $ms_id = $row[ms_id];
            $ms_one = true;
        ?>
            <strong>상품 : </strong>
            <input type="hidden" name="ms_id" value="<?=$row[ms_id]?>"> <?=$row[ms_name]?>
        <? } else { ?>
            <strong>상품 :</strong> 
            <select name="ms_id" required itemname="상품" onchange="location.href='<?=$PHP_SELF?>?ms_id='+this.value">
            <option value=""> 선택해주세요. </option>
            <option value=""> --------------- </option>
            <?  while ($row = sql_fetch_array($qry)) { ?>
            <? if ($ms_id == $row[ms_id]) $mem = $row; ?>
            <option value="<?=$row[ms_id]?>"> <?=$row[ms_name]?></option>
            <? } ?>
            </select>
            <script type="text/javascript"> document.fwrite.ms_id.value = "<?=$ms_id?>"; </script>
        <? } ?>
    </td></tr>
    <? if ($mem[ms_id] && trim($mem[ms_memo])) { ?>
    <tr><td>
        <div class="ms_memo"><?=$mem[ms_memo]?></div>
    </td></tr>
    <? } ?>
    <? if ($ms_id) { ?>
    <?
    $edate = false;
    $sql = "select * from $mw_cash[membership_log_table] where ms_id = '$ms_id' and mb_id = '$member[mb_id]' and ml_edate >= '$g4[time_ymdhis]' ";
    $sql.= "order by ml_edate desc limit 1";
    $row = sql_fetch($sql);
    if ($row) $edate = $row[ml_edate];
    ?>
    <tr><td>
        <?
        //$sql = "select * from $mw_cash[membership_item_table] where ms_id = '$ms_id' and mi_status = '1' order by mi_day desc";
        $sql = "select * from $mw_cash[membership_item_table] where ms_id = '$ms_id' and (mi_status = '1' or mi_renew = '1') order by mi_day";
        $qry = sql_query($sql);

        // --------------------------------------------------------------
        // 단일 상품일 경우 하나만 출력
        // --------------------------------------------------------------
        if (mysql_num_rows($qry) == 1) {
            $row = sql_fetch_array($qry);
            $mi_day = $row[mi_day];
            $mi_one = true;

            if ( ($row[mi_renew] && $edate) || !$row[mi_renew]) {
        ?>
            <strong>기간</strong> : 
            <input type="hidden" name="mi_day" value="<?=$row[mi_day]?>"> <?=$row[mi_day]?> 일 -
            <?
            $discount = 0;
            if ($row[mi_discount])
                $discount = mw_cash_membership_discount($row[mi_id]);

            if ($edate) { // 연장할인
                if ($row[mi_cash_extend] > $discount) {
                    $discount = $row[mi_cash_extend];
                    $row[mi_cash] = round($row[mi_cash] * ((100-$discount)/100));
                }
            }

            echo "$mw_cash[cf_cash_name] $row[mi_cash] $mw_cash[cf_cash_unit] ";

            if ($discount)
                echo " <s>".mw_cash_membership_original_cash($row[mi_id])."$mw_cash[cf_cash_unit]</s> ";

            echo "(".number_format(mw_cash_to_payment($row[mi_cash], $mw_cash[cf_tax]))."원";

            if ($discount)
                echo " <s>".number_format(mw_cash_to_payment(mw_cash_membership_original_cash($row[mi_id]), $mw_cash[cf_tax]))."원</s> ";

            if ($discount) {
                echo ", <span style=\"color:#ff0000;\">";
                if ($edate && $discount == $row[mi_cash_extend]) {
                    echo "연장";
                }
                echo "{$discount}% 할인</span>)";
            }
            else
                echo ")";
            ?>

            <? } ?>
            <?//="$mw_cash[cf_cash_name] $row[mi_cash] $mw_cash[cf_cash_unit] (".number_format(mw_cash_to_payment($row[mi_cash], $mw_cash[cf_tax]))."원)"?>
        <? }
        // --------------------------------------------------------------
        // 여러 상품일 경우 radio 버튼
        // --------------------------------------------------------------
        else { ?>
            <strong style="float:left;">기간 : </strong> 
            <div style="float:left;">
            <?/*
            <select name="mi_day" required itemname="기간">
            <option value=""> 선택해주세요. </option>
            <option value=""> --------------- </option>
            */?>
            <?  $row2 = sql_fetch("select mi_cash, mi_day from $mw_cash[membership_item_table] where ms_id = '$ms_id' order by mi_cash asc limit 1"); // 최저 ?>
            <?  while ($row = sql_fetch_array($qry)) { ?>
                <?
                if ($row[mi_renew]) {
                    $tmp = sql_fetch("select * from $mw_cash[membership_log_table] where ms_id = '$ms_id' and mb_id = '$member[mb_id]' and ml_day = '$row[mi_day]' and ml_edate >= '$g4[time_ymdhis]'");
                    if (!$tmp) continue;
                }
                ?>
                <? if ($row[mi_renew] && !$edate) continue; ?>
                <?/*
                <option value="<?=$row[mi_day]?>"> <?=$row[mi_day]?> 일 -
                <?="$mw_cash[cf_cash_name] $row[mi_cash] $mw_cash[cf_cash_unit] (".number_format(mw_cash_to_payment($row[mi_cash], $mw_cash[cf_tax]))."원)"?></option>
                */?>
                <div>
                <input type="radio" id="mi_day_<?=$row[mi_day]?>" name="mi_day" value="<?=$row[mi_day]?>" style="cursor:pointer;">
                <label for="mi_day_<?=$row[mi_day]?>" style="cursor:pointer;"><?=$row[mi_day]?> 일 -
                <?

                $discount = 0;
                if ($row[mi_discount])
                    $discount = mw_cash_membership_discount($row[mi_id]);

                if ($edate) { // 연장할인
                    if ($row[mi_cash_extend] > $discount) {
                        $discount = $row[mi_cash_extend];
                        $row[mi_cash] = round($row[mi_cash] * ((100-$discount)/100));
                    }
                }

                echo "$mw_cash[cf_cash_name] $row[mi_cash] $mw_cash[cf_cash_unit] ";

                if ($discount)
                    echo " <s>".mw_cash_membership_original_cash($row[mi_id])."$mw_cash[cf_cash_unit]</s> ";

                echo "(".number_format(mw_cash_to_payment($row[mi_cash], $mw_cash[cf_tax]))."원";

                if ($discount)
                    echo " <s>".number_format(mw_cash_to_payment(mw_cash_membership_original_cash($row[mi_id]), $mw_cash[cf_tax]))."원</s> ";

                if ($discount) {
                    echo ", <span style=\"color:#ff0000;\">";
                    if ($edate && $discount == $row[mi_cash_extend]) {
                        echo "연장시 ";
                    }
                    echo "{$discount}% 할인</span>)";
                }
                else
                    echo ")";
                ?>
                </label>
                </div>
            <? } ?>
            <?/* </select> */?>
            </div>
        <? } ?>
    </td></tr>
    <? if ($mw_cash[mb_cash]) { ?>
    <tr><td>
        <strong>나의 <?=$mw_cash[cf_cash_name]?> : </strong> <?=number_format($mw_cash[mb_cash])?> <?=$mw_cash[cf_cash_unit]?>
        [<a href="javascript:window.open('<?=$mw_cash[path]?>'); self.close();"><?=$mw_cash[cf_cash_name]?> 충전</a>]
    </td></tr>
    <? } ?>
    <?  if ($edate) { ?>
    <tr><td><strong>종료날짜 : </strong> <?=$edate?> <span style="color:#888; font-size:11px;">(종료날짜가 남았습니다! 추가하셔도 안심^^)</span></td></tr>
    <? } ?>
    </table>
    <? } ?>
</div>

<? if ($ms_id) { ?>
<div class="button">
    <? if ($mw_cash[mb_cash]) { ?> <input type="submit" value="가입하기" class="btn1"> &nbsp;&nbsp; <? } ?>
    <? if ($mw_cash[cf_card] || $mw_cash[cf_hp] ||  $mw_cash[cf_bank]) {  ?>
    <input type="button" value="바로결제" class="btn1" onclick="click_cash()"> &nbsp;&nbsp;
    <? } else { ?>
    <input type="button" value="<?=$mw_cash[cf_cash_name]?> 충전" class="btn1" onclick="window.open('<?=$mw_cash[path]?>'); self.close();">
    <? } ?>
</div>
<? } ?>
</form>

<?
$html = ob_get_contents();
ob_end_clean();

if ($ms_one && $mi_one && !$mw_cash[mb_cash])
    goto_url("buy-membership-charge.php?bo_table=$bo_table&ms_id=$ms_id&mi_day=$mi_day");
else
    echo $html;
?>

<script type="text/javascript">
function click_cash() {
    var ms_id = fwrite.ms_id.value;
    var mi_day = false;

    if (fwrite.mi_day.length > 0) {
        for (i=0; i<fwrite.mi_day.length; i++) {
            if (fwrite.mi_day[i].checked) {
                mi_day = fwrite.mi_day[i].value;
                break;
            }
        }
    } else {
        mi_day = fwrite.mi_day.value;
    }
    if (!mi_day) {
        alert("기간을 선택해주세요.");
        return;
    }
    location.href = "buy-membership-charge.php?bo_table=<?=$bo_table?>&ms_id=" + ms_id + "&mi_day=" + mi_day;
}
</script>

<?
include_once("$g4[path]/tail.sub.php");
?>
